import { Component, OnInit, Input, ViewChild } from '@angular/core';
declare var AVR
declare var THREE
declare var VR

@Component({
  selector: 'app-video-player',
  templateUrl: './video-player.component.html',
  styleUrls: ['./video-player.component.less']
})
export class VideoPlayerComponent {
  @Input() videoUrl = ''
  @ViewChild('main') main;
  vr = null
  barrageIsShow = false//是否显示弹幕
  constructor() { }

  ngAfterViewInit(): void {
    setTimeout(() => {
      this.init()
    }, 100);
  }
  init() {
    let scene, renderer;
    let container;
    AVR.debug = true;
    if (!AVR.Broswer.isIE() && AVR.Broswer.webglAvailable()) {
      renderer = new THREE.WebGLRenderer({
        antialias: true,
        alpha: true,
        maxLights: 10,
      });
    } else {
      renderer = new THREE.CanvasRenderer();
    }
    renderer.setPixelRatio(window.devicePixelRatio);
    container = this.main.nativeElement;
    container.appendChild(renderer.domElement);

    scene = new THREE.Scene();
    let vr;
    this.vr = vr = new VR(scene, renderer, container, { fov: 80 });

    //vr.playText="<img src='img/play90.png' width='40' height='40'/>";
    vr.vrbox.radius = 600;
    if (AVR.isCrossScreen()) {
      // 调整vr视窗偏移量
      vr.effect.separation = 1.2;
    }
    vr.loadProgressManager.onLoad = function () {
      // 视频静音
    };

    let playUrlArray = [
      {
        name: "录播",
        url: this.videoUrl,
      },
    ];
    AVR.useGyroscope = true;
    vr.onPlay = async () => {
    };
    playUrlArray.reverse();
    vr.init(playUrlArray, function () { }, false);
    vr.quality = playUrlArray[0].name;
    if (this.isType(playUrlArray[0].url, "m3u8")) {
      vr.play(playUrlArray[0].url, vr.resType.sliceVideo, { autoplay: false });
    } else {
      vr.play(playUrlArray[0].url, vr.resType.video, { autoplay: false });
    }
    vr.video.crossOrigin = "Anonymous";
  }
  isType(str, type) {
    if (str.split(".").pop() === type) {
      return true;
    } else {
      return false;
    }
  }
  ngOnDestroy() {
    if (this.vr) this.vr.destroy()
  }

}
